import meep as mp
import matplotlib.pyplot as plt
import numpy as np
import copy
mp.quiet(True)
# Initialized parameter
b = 1 # base of the square (On peut prend l'unité) 10 pixel
pml = 0.02 # the thickness of pml layer
pad = 0.03 # the padding between the cavity and the pml layer
n = 1.5 # refractive index the cavity (used resin)
resolution = 200
len_scale = 25
sxy = b*np.sqrt(2) + 2*pml + 2*pad
cell_size = mp.Vector3(sxy, sxy, 0)
# Booudary conditions (using perfect matched layer)
boudary_layer = mp.PML(pml)
# Geometry of the cavity (rotated 45 degree)
geometry = [mp.Block(mp.Vector3(b,b,mp.inf),
mp.Vector3(1,1,0),
mp.Vector3(1,-1,0),
center=mp.Vector3(),
material=mp.Medium(index=n))]
# Injected sources
wl = 4 # wavelenght in micrometer / in vaccum
fcen = len_scale/wl # pulse center frequency
df = 20 # pulse frequency width
pos_x = 0.25
pos_y = 0.41
src_centers = [mp.Vector3(-pos_x,pos_y), mp.Vector3(pos_x,pos_y),
mp.Vector3(pos_x,-pos_y), mp.Vector3(-pos_x,-pos_y)]
src_size = mp.Vector3(0)
src_amps = [1,-1,-1,1]
src_compt = mp.Ez
src = [mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[0], src_size, src_amps[0]),
mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[1], src_size, src_amps[1]),
mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[2], src_size, src_amps[2]),
mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[3], src_size, src_amps[3])]
#Simulation
sim = mp.Simulation(cell_size= cell_size,
geometry= geometry,
sources=src,
symmetries=[mp.Mirror(mp.X, phase = -1), mp.Mirror(mp.Y)],
resolution=resolution,
boundary_layers=[boudary_layer])
# visualize the cavity
f = plt.figure(dpi=150)
sim.plot2D(ax = f.gca())
x0 = [-0.37]
y0 = [0.12]
plt.plot(x0, y0, "o")
plt.show()
# define step function to collect field Ez
def append_fields(sim):
box = mp.Volume(center=pt_observed, size=mp.Vector3(0,0))
ez_fields.append(sim.get_array(vol=box, component=mp.Ez))
ez_fields = []
pt_observed = mp.Vector3(-0.37,0.12)
sim.reset_meep()
sim.run(mp.after_sources(append_fields),
until_after_sources=250)
ez0 = ez_fields.copy()
plt.figure()
plt.plot(ez0)
plt.title('Ez')
plt.show()
np.savez('data/Ez_mixed_t250', np.array(ez0))
%matplotlib notebook
path = "/home/hong/Desktop/Microlaser1/data/"
file = "modes_Ez_mixed.npz"
npzfile = np.load(path + file)
modes = npzfile['arr_0']
plt.figure()
plt.scatter(modes[:,1], -modes[:,1]/2/modes[:,0])
plt.xlabel('$2\pi \omega_n$')
plt.ylabel('$Im(-2 \pi \omega_n)$')
plt.grid()
plt.title('modes de résonance')
x1 = [1.42423743e+01]
y1 = [-1.42423743e+01/2/4.75829261e+03 ]
x2 = [1.42478841e+01]
y2 = [-1.42478841e+01/2/6.80704538e+02]
x3 = [1.42612746e+01]
y3 = [-1.42612746e+01/2/2.37503253e+02]
plt.plot(x1, y1, "rs", x2, y2, "gs", x3,y3,"ys")
plt.show()
print(modes[(modes[:,1] > 14)
& (modes[:,1] < 14.5)
#& (modes[:,0]>100)
])
def run_mode(fcen, df, until) :
# overwrite parameter of simulation
fcen = fcen
df = df
src = [mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[0], src_size, src_amps[0]),
mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[1], src_size, src_amps[1]),
mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[2], src_size, src_amps[2]),
mp.Source(mp.GaussianSource(fcen, fwidth=df), src_compt, src_centers[3], src_size, src_amps[3])]
sim.reset_meep() # reset everything
sim.sources = src
sim.run(mp.after_time(0, append_fields),
until_after_sources = until) # run until steady state
%matplotlib inline
pt_observed = mp.Vector3(-0.28,0.32) # for function append_fields
ez_fields = [] # reset field
run_mode(fcen = 1.42423743e+01, df = 0.001, until = 100)
# Plot propagating field
ez1 = ez_fields.copy()
plt.figure()
#plt.axis([0.5,0.6,-17,17])
plt.plot(ez1)
plt.title('Ez')
plt.show()
# Plot field Ez in the last instant
plt.figure(figsize=(12,9))
x0 = [-0.28]
y0 = [0.32]
plt.plot(x0, y0, "s")
sim.plot2D(fields = mp.Ez)
freq = 1.42423743e+01
f = plt.figure(dpi=150)
Animate = mp.Animate2D(sim, fields=mp.Ez, f=f, realtime=False, normalize=True)
sim.run(mp.at_every(1/freq/20,Animate),until=1/freq)
plt.close()
Animate.to_jshtml(5)
ez_fields = []
run_mode(fcen = 1.42478841e+01, df = 0.001, until = 100)
ez2 = ez_fields.copy()
plt.figure()
#plt.axis([0.5,0.6,-17,17])
plt.plot(ez2)
plt.title('Ez')
plt.show()
plt.figure(figsize=(12, 9))
x0 = [-0.28]
y0 = [0.32]
plt.plot(x0, y0, "s")
sim.plot2D(fields = mp.Ez)
freq = 1.42478841e+01
f = plt.figure(dpi=150)
Animate = mp.Animate2D(sim, fields=mp.Ez, f=f, realtime=False, normalize=True)
sim.run(mp.at_every(1/freq/20,Animate),until=1/freq)
plt.close()
Animate.to_jshtml(5)
ez_fields = []
run_mode(fcen = 1.42612746e+01, df = 0.005, until = 100)
ez3 = ez_fields.copy()
plt.figure()
#plt.axis([0.5,0.6,-17,17])
plt.plot(ez3)
plt.title('Ez')
plt.show()
plt.figure(figsize=(12, 9))
x0 = [-0.28]
y0 = [0.33]
plt.plot(x0, y0, "s")
sim.plot2D(fields = mp.Ez)
freq = 1.42612746e+01
f = plt.figure(dpi=150)
Animate = mp.Animate2D(sim, fields=mp.Ez, f=f, realtime=False, normalize=True)
sim.run(mp.at_every(1/freq/20,Animate),until=1/freq)
plt.close()
Animate.to_jshtml(5)